package com.spc.car.data.tool;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.SqlServerTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;

public class GeneratorServiceEntity {

    @Test
    public void generateMSSQLCode() {
        String packageName = "com.kmcs.kmcstp";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService

        String dbUrl = "jdbc:sqlserver://39.108.84.106:3248; DatabaseName=bid_new";

        generateByTables(serviceNameStartWithI, dbUrl, "kmcs_sa", "51ztb8899!@#",
                packageName,DbType.SQL_SERVER,
                "ios_price");
    }

    @Test
    public void generateMYSQLCode() {
        String packageName = "com.spc.oa.data";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService

        generateByTables(serviceNameStartWithI, "jdbc:mysql://120.78.80.22:3306/kmcs_oa", "root", "v88jF5SYh4F8EPsE",
                packageName,DbType.MYSQL,
                "adm_vehicle_record", "adm_private_vehicle_info", "adm_private_vehicle_record");

    }

    private void generateByTables(boolean serviceNameStartWithI, String dbUrl, String user,
                                  String password,String packageName, DbType dbtype,
                                  String... tableNames) {
        GlobalConfig config = new GlobalConfig();

        String driverName = "";
        if(DbType.MYSQL.equals(dbtype)) {
            driverName = "com.mysql.cj.jdbc.Driver";
        }
        else if(DbType.SQL_SERVER.equals(dbtype)) {
            driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        }

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(dbtype)
                .setUrl(dbUrl)
                .setUsername(user)
                .setPassword(password)
                .setDriverName(driverName);

        //if(DbType.SQL_SERVER.equals(dbtype)) {
        if(DbType.MYSQL.equals(dbtype)){
            dataSourceConfig.setTypeConvert(new SqlServerTypeConvert(){
                // 自定义数据库表字段类型转换【可选】
                @Override
                public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                    if(fieldType.toLowerCase().contains("decimal")) {
                        return DbColumnType.BIG_DECIMAL;
                    }
                    if(fieldType.toLowerCase().contains("datetime")) {
                        return DbColumnType.DATE;
                    }
                    if(fieldType.toLowerCase().contains("numeric")) {
                        System.out.println("fieldType:" + fieldType);
                    }
                    return super.processTypeConvert(globalConfig, fieldType);
                }
            });
        }

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组

        config.setAuthor("")
                .setEnableCache(false)
                .setOutputDir("d:\\codeGen")
                .setFileOverride(true);

        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

}
